package lily.com.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class EditActivity extends Activity implements View.OnClickListener {

	private int tid = -1;

	private EditText txtTitle = null;
	private EditText txtText = null;
	private Button btnSave = null;
	private Button btnCancel = null;
	private Button btnDiscard = null;
	private TaskDao taskDao = null;
	private TaskDomain taskDomain = null;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (!SQLiteManager.isOpen())
			SQLiteManager.open(this);
		Bundle bundle = this.getIntent().getExtras();
		tid = bundle.getInt(TaskDao.TaskDaoFields.COL_NAME_TID);
//		Log.d("DEBUG", "tid=" + tid);
		setupViews();
	}

	@Override
	protected void onResume() {
		if (!SQLiteManager.isOpen())
			SQLiteManager.open(this);
		super.onResume();
	}

	@Override
	protected void onDestroy() {
		if (SQLiteManager.isOpen())
			SQLiteManager.close();
		super.onDestroy();
	}
	
	@Override
	protected void onPause() {
		TaskListViewAdapter adpater = TaskListViewAdapter.getInstance(this);
		adpater.flushRowsIntoDB();
		super.onPause();
		super.onPause();
	}

	public void setupViews() {
		setContentView(R.layout.activity_edit);
		txtTitle = (EditText) findViewById(R.id.txtTitle);
		txtText = (EditText) findViewById(R.id.txtText);
		btnSave = (Button) findViewById(R.id.btnSave);
		btnCancel = (Button) findViewById(R.id.btnCancel);
		btnDiscard = (Button) findViewById(R.id.btnDiscard);
		btnSave.setOnClickListener(this);
		btnCancel.setOnClickListener(this);
		btnDiscard.setOnClickListener(this);
		taskDao = new TaskDao();

		if (tid > 0) {
			taskDomain = taskDao.selectTask(tid);
			if (txtTitle == null || txtText == null || taskDomain == null) {
//				Log.d("DEBUG", "null pointer");
			}
			txtTitle.setText(taskDomain.getTitle());
			txtText.setText(taskDomain.getText());
		}
	}

	@Override
	public void onClick(View v) {
		int vid = v.getId();
		switch (vid) {
		case R.id.btnSave:
			btnSaveOnClick();
			break;
		case R.id.btnCancel:
			btnCancelOnClick();
			break;
		case R.id.btnDiscard:
			btnDiscardOnClick();
			break;
		default:
			break;
		}
	}

	private void btnDiscardOnClick() {
		TaskListViewAdapter adapter = TaskListViewAdapter.getInstance(this);
		adapter.deleteTask(tid);
		finish();
	}

	private void btnCancelOnClick() {
		finish();
	}

	private void btnSaveOnClick() {
		String title = txtTitle.getText().toString();
		String text = txtText.getText().toString();
		TaskListViewAdapter adapter = TaskListViewAdapter.getInstance(this);
		boolean isFinish = true;

		if (null == title || "".equals(title)) {
			AlertDialog dialog = new AlertDialog.Builder(this)
					.setTitle("请输入新建任务的标题后，按确定\n或者取消新建任务")
					.setPositiveButton("确定",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int whichButton) {

								}
							}).create();
			dialog.show();
			isFinish = false;
		}

		if (tid == -1) {
//			Log.d("DEBUG", "btnSaveOnClick() insert");
			adapter.insertTask(title, text, TaskDomain.TaskStatus.NONE);
		} else {
//			Log.d("DEBUG", "btnSaveOnClick() update");
			adapter.updateTask(taskDomain.getTid(), title, text,
					taskDomain.getStatus());
		}

		if (isFinish)
			finish();

	}
	
}
